package com.wangyi.fightimg.moduls.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.wangyi.fightimg.R;
import com.wangyi.fightimg.utils.http.ServiceUrl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangYi
 *         首页列表适配器
 */

public class MainFragmentAdapter extends RecyclerView.Adapter<MainFragmentAdapter.MyViewHolder> {

    private Context context;
    private List<MainFragmentBean> beanList;// 列表项数据实体类集合
    private OnItemClickListener onItemClickListenern;// 列表项点击事件
    private int loadCount;// 加载列表项的个数
    private boolean isFirest = true;// 是否首次加载

    public MainFragmentAdapter(Context context, List<MainFragmentBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    public void setCheckChangeListtener(OnItemClickListener onItemClickListenern) {
        this.onItemClickListenern = onItemClickListenern;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            holder.rbPraise.setChecked(beanList.get(position).isCheck());
            holder.rbPraise.setText(String.valueOf(beanList.get(position).getCount()));
        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final MainFragmentBean mainFragmentBean = beanList.get(position);
        String imgType = mainFragmentBean.getUrl().substring(beanList.get(position).getUrl().lastIndexOf(".") + 1);
        // 展示图片
        if (!TextUtils.isEmpty(imgType)) {
            if ("gif".equals(imgType)) {// 展示gif图片
                Glide
                        .with(context)
                        .load(ServiceUrl.BASE_IMG + mainFragmentBean.getUrl())
                        .asGif()
                        .listener(new RequestListener<String, GifDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<GifDrawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GifDrawable resource, String model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                if (isFirest) {
                                    loadCount++;
                                    if (loadCount == beanList.size()) {
                                        isFirest = false;
                                        notifyItemChanged(0);
                                    }
                                }
                                return false;
                            }
                        })
                        .error(R.mipmap.ic_launcher)
                        .placeholder(R.mipmap.ic_launcher)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .dontTransform()
                        .dontAnimate()
                        .into(holder.ivImg);
            } else {// 展示静态图片
                Glide
                        .with(context)
                        .load(ServiceUrl.BASE_IMG + mainFragmentBean.getUrl())
                        .asBitmap()
                        .listener(new RequestListener<String, Bitmap>() {
                                      @Override
                                      public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                                          return false;
                                      }

                                      @Override
                                      public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                          if (isFirest) {
                                              loadCount++;
                                              if (loadCount == beanList.size()) {
                                                  isFirest = false;
                                                  notifyItemChanged(0);
                                              }
                                          }
                                          return false;
                                      }
                                  }
                        )
                        .error(R.mipmap.ic_launcher)
                        .placeholder(R.mipmap.ic_launcher)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .dontTransform()
                        .dontAnimate()
                        .into(holder.ivImg);
            }
        }
        // 设置赞图标样式
        if (mainFragmentBean.isCheck()) {
            holder.rbPraise.setChecked(true);
        } else {
            holder.rbPraise.setChecked(false);
        }
        // 点赞人数
        holder.rbPraise.setText(String.valueOf(mainFragmentBean.getCount()));
        holder.ivShare.setTag(ServiceUrl.BASE_IMG + mainFragmentBean.getUrl());
        // 赞点击事件
        holder.rbPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mainFragmentBean.isCheck()) {
                    onItemClickListenern.setOnClickListener(holder.rbPraise, holder.getLayoutPosition());
                }
            }
        });
        // 分享点击事件
        holder.ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListenern.setOnClickListener(holder.ivShare, holder.getLayoutPosition());
            }
        });

    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;// 图片
        RadioButton rbPraise;// 赞
        ImageView ivShare;// 分享
        LinearLayout llRoot;// 根布局

        public MyViewHolder(View itemView) {
            super(itemView);
            llRoot = (LinearLayout) itemView.findViewById(R.id.ll_root);
            ivImg = (ImageView) itemView.findViewById(R.id.iv_img);
            rbPraise = (RadioButton) itemView.findViewById(R.id.rb_praise);
            ivShare = (ImageView) itemView.findViewById(R.id.iv_share);
        }
    }

    public interface OnItemClickListener {
        void setOnClickListener(View view, int position);
    }
}
